package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.convert.HotelConverter;
import com.travel.convert.RoomConverter;
import com.travel.dto.HotelDto;
import com.travel.dto.HotelOrderDto;
import com.travel.feign.EsFeign;
import com.travel.feign.OrderFeign;
import com.travel.mapper.ImagesMapper;
import com.travel.mapper.RoomMapper;
import com.travel.pojo.Hotel;
import com.travel.pojo.Room;
import com.travel.service.HotelService;
import com.travel.mapper.HotelMapper;
import com.travel.vo.HotelVo;
import com.travel.vo.RoomVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 18757
* @description 针对表【hotel(酒店表)】的数据库操作Service实现
* @createDate 2023-05-09 16:35:04
*/
@Service
public class HotelServiceImpl extends ServiceImpl<HotelMapper, Hotel>
    implements HotelService{
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private HotelMapper hotelMapper;
    @Resource
    private ImagesMapper imagesMapper;
    @Resource
    private EsFeign esFeign;
    @Resource
    private OrderFeign orderFeign;

    /**
     * 根据地区或酒店名查找酒店
     * @param keyWord    地区name 或 hotel name
     * @param pageNumber 页码
     * @param pageSize   每页数据个数
     * @return 分页的 hotel
     */
    @Override
    public List<HotelVo> selectHotelByName(String keyWord, Integer star,int pageNumber, int pageSize) {
        List<Hotel> search = esFeign.search(keyWord, pageNumber, pageSize).getContent();
        System.out.println("=-=");
        System.out.println(search);
        System.out.println("=-=");
        // 判定星级 是否 存在 ， 不存在则不加入筛选
        if(star != null && star != 0){
            search = search.stream().filter(hotel -> Objects.equals(hotel.getStar(), star)).collect(Collectors.toList());
        }

        List<HotelVo> collect = search.stream().map(HotelConverter.INSTANCE::entityToVo).collect(Collectors.toList());
        return collect.stream().peek(hotelVo -> hotelVo.setImages(imagesMapper.selectImagesByForeignId("hotel" + hotelVo.getId()))).collect(Collectors.toList());
    }

    /**
     * 根据酒店id 获取与之关联的房间
     *
     * @param id 酒店id
     * @return 房间集合
     */
    @Override
    public List<RoomVo> selectRoomOfHotel(Integer id) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hotel_id",id).ge("remaining_room",1);
        List<Room> rooms = roomMapper.selectList(queryWrapper);
        List<RoomVo> collect = rooms.stream().map(RoomConverter.INSTANCE::entityToVo).collect(Collectors.toList());
        List<RoomVo> collect1 = collect.stream().peek(roomVo -> roomVo.setRoomImages(imagesMapper.selectImagesByForeignId("room" + roomVo.getId()))).collect(Collectors.toList());
        System.out.println(collect1);
        return collect1;
    }

    /**
     * 根据房间id 查找房间详细信息
     *
     * @param id 房间id
     * @return 房间信息
     */
    @Override
    public RoomVo selectRoomByRoomId(Integer id) {
        Room room = roomMapper.selectById(id);
        RoomVo roomVo = RoomConverter.INSTANCE.entityToVo(room);
        roomVo.setRoomImages(imagesMapper.selectImagesByForeignId("room"+roomVo.getId()));
        return roomVo;
    }

    /**
     * 根据酒店id 获取酒店的详细信息
     *
     * @param id 酒店id
     * @return 酒店信息
     */
    @Override
    public HotelVo selectHotelByHotelId(Integer id) {
        Hotel hotel = hotelMapper.selectById(id);
        HotelVo hotelVo = HotelConverter.INSTANCE.entityToVo(hotel);
        hotelVo.setImages(imagesMapper.selectImagesByForeignId("hotel" + hotelVo.getId()));
        return hotelVo;
    }

    /**
     * 根据条件查询酒店
     * @param hotelDto 条件对象
     * @return 酒店集合
     */
    @Override
    public List<HotelVo> selectHotelCondition(HotelDto hotelDto) {
        List<Hotel> hotels = hotelMapper.selectHotelCondition(hotelDto);
        System.out.println(hotels);
        // 判断 设施列表是否空,不是的话则过滤出符合用户选择的设施的酒店
        if(hotelDto.getFacility().length > 0) {
            hotels = hotels.stream().filter(o -> {
                Integer[] integers = Arrays.stream(o.getFacility().split(",")).map(Integer::parseInt).toArray(Integer[]::new);
                // 修改数组中的顺序
                Arrays.sort(integers);
                Arrays.sort(hotelDto.getFacility());
                return Arrays.stream(hotelDto.getFacility()).allMatch(value -> Arrays.asList(integers).contains(value));
            }).collect(Collectors.toList());
        }

        // 返回vo 插入图片
        return hotels.stream().map(hotel -> {
            HotelVo hotelVo = HotelConverter.INSTANCE.entityToVo(hotel);
            List<String> images = imagesMapper.selectImagesByForeignId("hotel" + hotelVo.getId());
            hotelVo.setImages(images != null ? images:new ArrayList<>());
            return hotelVo;
        }).collect(Collectors.toList());
    }

    /**
     * 创建订单
     *
     * @param hotelOrderDto 订单属性
     * @return
     */
    @Override
    public HashMap<String, String> createHotelOrder(HotelOrderDto hotelOrderDto) {
        return orderFeign.generateOrder(hotelOrderDto);
    }

    /**
     * 获取订单id
     * @return 订单id
     */
    @Override
    public String getOrderId() {
        return orderFeign.createOrder();
    }

    @Override
    public List<HotelVo> selectHotelAll() {
        List<Hotel> hotels = hotelMapper.selectList(null);
        return hotels.stream().map(o -> {
            HotelVo hotelVo = HotelConverter.INSTANCE.entityToVo(o);
            hotelVo.setImages(imagesMapper.selectImagesByForeignId("hotel" + hotelVo.getId()));
            return hotelVo;
        }).collect(Collectors.toList());
    }
}




